package com.softer.ztb.controller;

import com.softer.ztb.common.entity.ShiroCommonResult;
import com.softer.ztb.common.enums.ShiroMessageEnum;
import com.softer.ztb.common.enums.ShiroResultEnum;
import com.softer.ztb.entity.SysUser;
import com.softer.ztb.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
/**
 * LoginController class
 * @author zcz
 * @date 2019/1/16
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 退出
     * @return
     */
    @GetMapping("/logout")
    public ShiroCommonResult logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return new ShiroCommonResult(ShiroResultEnum.SUCCESS.getCode(), ShiroMessageEnum.SHIRO_AUTHC_LOGOUT_MESSAGE.getMsg());
    }

    /**
     * 登录
     * @param userName
     * @param passWord
     * @param rememberMe
     * @param session
     * @return
     */
    @RequestMapping("/loginUser")
    public ShiroCommonResult loginUser(@RequestParam("userName") String userName,
                            @RequestParam("passWord") String passWord,boolean rememberMe,
                            HttpSession session) {
        System.out.println("rememberMe:"+rememberMe);
        log.info("登录开始，用户名："+userName);
        UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord,rememberMe);
        Subject subject = SecurityUtils.getSubject();
        log.info("登录结束！");
        return exceptionMessage(subject,token,session);
    }

    public ShiroCommonResult exceptionMessage(Subject subject,UsernamePasswordToken token,HttpSession session){
        try{
            subject.login(token);
            SysUser user = (SysUser) subject.getPrincipal();
            session.setAttribute("userName", user.getUserName());
            loginService.updateLastLoginTime(user.getUserName());
            return new ShiroCommonResult(ShiroResultEnum.SUCCESS.getCode(), ShiroMessageEnum.SHIRO_AUTHC_LOGIN_MESSAGE.getMsg());
        }catch (UnknownAccountException unknownAccountException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_UNKNOWNACCOUNTEXCEPTION_MESSAGE.getMsg());
        }catch (IncorrectCredentialsException incorrectCredentialsException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_INCORRECTCREDENTIALSEXCEPTION_MESSAGE.getMsg());
        }catch (LockedAccountException lockedAccountException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_LOCKEDACCOUNTEXCEPTION_MESSAGE.getMsg());
        }catch (DisabledAccountException disabledAccountException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_DISABLEDACCOUNTEXCEPTION_MESSAGE.getMsg());
        }catch (ExcessiveAttemptsException excessiveAttemptsException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_EXCESSIVEATTEMPTSEXCEPTION_MESSAGE.getMsg());
        }catch (ConcurrentAccessException concurrentAccessException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_CONCURRENTACCESSEXCEPTION_MESSAGE.getMsg());
        }catch (AccountException accountException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_ACCOUNTEXCEPTION_MESSAGE.getMsg());
        }catch (UnsupportedTokenException unsupportedTokenException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_UNSUPPORTEDTOKENEXCEPTION_MESSAGE.getMsg());
        }catch (CredentialsException credentialsException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_CREDENTIALSEXCEPTION_MESSAGE.getMsg());
        }catch (AuthenticationException authenticationException){
            return new ShiroCommonResult(ShiroResultEnum.FAIL.getCode(), ShiroMessageEnum.SHIRO_AUTHC_AUTHENTICATIONEXCEPTION_MESSAGE.getMsg());
        }
    }

}
